<?php
/**
 * （Logic）
 */
namespace App\Logic\Amazon;

use App\Models\Amazon\FbaCustomerReturn;
use App\Models\Amazon\FbaRemovalOrderDetailCondition;
use App\Models\Amazon\ReportRequest;
use App\Models\Basics\AmazonAccount;
class GetReportWork
{
    private static function _getAccounts($accountId = 0){
        $where = $accountId ? ['id' => $accountId] : ['api' => 1];
        return AmazonAccount::optionsForApi($where);
    }

    /**
     * @param int $accountId
     * 物流管理库存报告（每日库存报告）
     */
    public static function myiInventoryReportRequest($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        foreach ($accounts as $account){
            $re[$account->id] = GrabReport::requestReport($account,3);
        }
        var_dump($re);
    }

    /**
     * @param int $accountId
     * 保留库存报告
     */
    public static function reservedInventoryReportRequest($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        foreach ($accounts as $account){
            $re[$account->id] = GrabReport::requestReport($account,13);
        }
        var_dump($re);
    }

    /**
     * @param int $accountId
     * 库存报告 废弃
     */
    /*public static function afnInventoryReportRequest($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        foreach ($accounts as $account){
            $re[$account->id] = GrabReport::requestReport($account,9);
        }
        var_dump($re);
    }*/

    /**
     * 收货报告
     * @param int $accountId
     */
    public static function fulfillmentInventoryReportRequest($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        $maxRequestDate = ReportRequest::getRequestDate(1);
        $start = date('Y-m-d',strtotime('-3 days',strtotime($maxRequestDate)));
        $end = date('Y-m-d');
        foreach ($accounts as $account){
            $con = [];
            $con['StartDate'] = getDateFormat($start);
            $con['EndDate'] = getDateFormat($end);
            $re[$account->id] = GrabReport::requestReport($account,1,$con);
        }
        var_dump($re);
    }

    /**
     * 退货报告
     * @param int $accountId
     */
    public static function fulfillmentCustomerReturnsReportRequest($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        $maxReturnDate = FbaCustomerReturn::query()->max('return_date');
        $start = date('Y-m-d',strtotime('+1 days',strtotime($maxReturnDate)));
        $end = date('Y-m-d');
        foreach ($accounts as $account){
            $con = [];
            $con['StartDate'] = getDateFormat($start);
            $con['EndDate'] = getDateFormat($end);
            $re[$account->id] = GrabReport::requestReport($account,4,$con);
        }
        var_dump($re);
    }

    /**
     * 移库报告 废弃
     * @param int $accountId
     */
    //public static function fulfillmentRemovalShipmentReportRequest($accountId = 0)
    //{
    //    $accounts = self::_getAccounts($accountId);
    //    $re = [];
    //    $start = FbaRemovalShipmentCondition::getRequestDate();
    //    $end = date('Y-m-d');
    //    foreach ($accounts as $account){
    //        $con = [];
    //        $con['StartDate'] = getDateFormat($start);
    //        $con['EndDate'] = getDateFormat($end);
    //        $re[$account->id] = GrabReport::requestReport($account,5,$con);
    //    }
    //    var_dump($re);
    //}

    /**
     * 移库明细报告
     * @param int $accountId
     */
    public static function fulfillmentRemovalDetailReportRequest($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        $start = FbaRemovalOrderDetailCondition::getRequestDate();
        $end = date('Y-m-d');
        foreach ($accounts as $account){
            $con = [];
            $con['StartDate'] = getDateFormat($start);
            $con['EndDate'] = getDateFormat($end);
            $re[$account->id] = GrabReport::requestReport($account,11,$con);
        }
        var_dump($re);
    }

    /**
     * 转款报告
     * @param int $accountId
     */
    public static function settlementDepositReportList($accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
        $end = date('Y-m-d');
        $start = date('Y-m-d',strtotime('-2 days',strtotime($end)));
        foreach ($accounts as $account){
            $con = [];
            $con['AvailableFromDate'] = getDateFormat($start);
            $con['AvailableToDate'] = getDateFormat($end);
            $con['typeIds'] = 12;
            $re[$account->id] = GrabReport::getReportList($account,$con);
        }
        var_dump($re);
    }

    /**
     * TEST报告
     * @param int $typeId
     * @param int $accountId
     */
    public static function testReport($typeId,$accountId = 0)
    {
        $accounts = self::_getAccounts($accountId);
        $re = [];
//        $end = date('Y-m-d');
//        $start = date('Y-m-d',strtotime('-2 days',strtotime($end)));
        // $end = '2022-05-01';
        // $start = '2022-04-01';
        foreach ($accounts as $account){
            $con = [];
//            $con['StartDate'] = getDateFormat($start);
//            $con['EndDate'] = getDateFormat($end);
            $re[$account->id] = GrabReport::requestReport($account,$typeId,$con);
        }
        var_dump($re);
    }

    /**
     * 获取报告
     */
    public static function getReport(){
        $accounts = self::_getAccounts();
        $re = [];
        foreach ($accounts as $account){
            $re[$account->id] = GrabReport::getReport($account);
        }
        var_dump($re);
    }
}
